//
//  FriendInfoView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/28.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "FriendInfoView.h"

#import "JoinCourseCollectionViewCell.h"

#define FONT_COLOR RGB_COLOR(164, 164, 164, 1)

static NSString * const kJoinCourseCollectionViewCellIdentifier = @"joinCourseCollectionViewCellIdentifier";

@interface FriendInfoView ()<UICollectionViewDataSource>

@property (nonatomic, strong) UIView * whiteView; // 中间的白色视图
@property (nonatomic, strong) UIImageView * headImageView; // 头像视图
@property (nonatomic, strong) UILabel * nameLabel; // 昵称
@property (nonatomic, strong) UIView * graySeparaView; // 灰色分隔线
@property (nonatomic, strong) UIButton * quitButton; // 退出按钮
@property (nonatomic, strong) UILabel * joinCourseLabel; // 参加的课程
@property (nonatomic, strong) UICollectionViewFlowLayout * layout; // 流式布局
@property (nonatomic, strong) UICollectionView * joinCourseCollectionView; // 参加的课程集合视图
@property (nonatomic, strong) NSArray * dataSource; // 数据源

@end

@implementation FriendInfoView

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithFriendid:(NSString *)friendid
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGB_COLOR(0, 0, 0, 0.3);
        
        // 添加视图
        [self addSubview:self.whiteView];
        [_whiteView addSubview:self.headImageView];
        [_whiteView addSubview:self.nameLabel];
        [_whiteView addSubview:self.graySeparaView];
        [_whiteView addSubview:self.quitButton];
        [_whiteView addSubview:self.joinCourseLabel];
        [_whiteView addSubview:self.joinCourseCollectionView];
        
        // 自动布局
        [_whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self).multipliedBy(0.53);
            make.height.equalTo(_whiteView.mas_width).multipliedBy(0.7);
            make.centerX.equalTo(self).offset(SCREEN_WIDTH * 0.195 * 0.5);
            make.centerY.equalTo(self);
        }];
        [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(_whiteView.mas_width).multipliedBy(0.1);
            make.centerX.equalTo(_whiteView).multipliedBy(0.15);
            make.centerY.equalTo(_whiteView).multipliedBy(0.18);
        }];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_whiteView).multipliedBy(0.4);
            make.centerY.equalTo(_whiteView).multipliedBy(0.1);
        }];
        [_graySeparaView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_whiteView);
            make.height.offset(1);
            make.centerX.equalTo(_whiteView);
            make.centerY.equalTo(_whiteView).multipliedBy(0.37);
        }];
        [_quitButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(_whiteView.mas_width).multipliedBy(0.04);
            make.top.offset(5);
            make.right.offset(-5);
        }];
        [_joinCourseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headImageView).offset(5);
            make.centerY.equalTo(_whiteView).multipliedBy(0.53);
        }];
        [_joinCourseCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_whiteView).multipliedBy(0.94);
            make.height.equalTo(_whiteView).multipliedBy(0.65);
            make.centerX.equalTo(_whiteView);
            make.top.equalTo(_joinCourseLabel.mas_bottom).offset(SCREEN_HEIGHT * 0.02);
        }];
        
        // 加载数据
        [self loadData:friendid];
    }
    return self;
}

#pragma mark 🌷🌷🌷UICollectionViewDataSource🌷🌷🌷
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JoinCourseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kJoinCourseCollectionViewCellIdentifier forIndexPath:indexPath];
    
    // cell 赋值
    cell.videoNameLabel.text = _dataSource[indexPath.row][@"lname"];
    [cell.videoCoverImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(_dataSource[indexPath.row][@"img"])]];
    cell.teacherNameLabel.text = [NSString stringWithFormat:@"讲师：%@", _dataSource[indexPath.row][@"teacher"]];
    cell.countLessionLabel.text = [NSString stringWithFormat:@"共%ld课时", [_dataSource[indexPath.row][@"countLession"] integerValue]];
    cell.studentsLabel.text = [NSString stringWithFormat:@"%ld人正在学习", [_dataSource[indexPath.row][@"student"] integerValue]];
    cell.priceLabel.text = [_dataSource[indexPath.row][@"price"] integerValue] == 0 ? @"免费课程" : [NSString stringWithFormat:@"%.2f", [_dataSource[indexPath.row][@"price"] floatValue]];
    cell.discussLabel.text = [NSString stringWithFormat:@"%ld评价", [_dataSource[indexPath.row][@"discuss"] integerValue]];
    [cell.starsView addSubview:[[StarsView alloc] initWithLevelInt:[_dataSource[indexPath.row][@"levelInt"] integerValue]]];
    
    return cell;
}

#pragma mark 🌷🌷🌷event response🌷🌷🌷
- (void)respondsToQuitButton {
    [self removeFromSuperview];
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)loadData:(NSString *)friendid {
    NSMutableDictionary * paramters = [NSMutableDictionary dictionary];
    [paramters setObject:friendid forKey:@"id"];
    [paramters setObject:@(10000) forKey:@"pageNum"];
    
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(PERSONALHOMEPAGE_SYSTEMMESSAGE_FRIENDINFO_URL) parameters:paramters finish:^(id responseObject, NSError *error) {
        NSLog(@"%@", responseObject);
        if (!error) {
            if ([responseObject[@"status"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"加载完成！"];
                [self loadSuccess:responseObject[@"data"]];
            } else {
                [MBProgressHUD showError:@"加载失败！"];
            }
        } else {
            [MBProgressHUD showError:[[NSString stringWithFormat:@"%@", error] containsString:@"The request timed out."] ? @"登录超时" : @"I don't know"];
        }
    }];
}
- (void)loadSuccess:(NSDictionary *)data {
    // 用户信息
    NSDictionary * userInfo = data[@"userinfo"];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(userInfo[@"head"])] placeholderImage:[UIImage imageNamed:@"default_head_s.png"]];
    _nameLabel.text = userInfo[@"username"];
    
    // 课程信息
    _dataSource = data[@"lesson"];
    [_joinCourseCollectionView reloadData];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc] init];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView;
}
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.backgroundColor = [UIColor redColor];
    }
    return _headImageView;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = FONT_COLOR;
        _nameLabel.font = [UIFont systemFontOfSize:25];
    }
    return _nameLabel;
}
- (UIView *)graySeparaView {
    if (!_graySeparaView) {
        _graySeparaView = [[UIView alloc] init];
        _graySeparaView.backgroundColor = RGB_COLOR(164, 164, 164, 1);
    }
    return _graySeparaView;
}
- (UIButton *)quitButton {
    if (!_quitButton) {
        _quitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quitButton setBackgroundImage:[UIImage imageNamed:@"close_btn.png"] forState:UIControlStateNormal];
        [_quitButton addTarget:self action:@selector(respondsToQuitButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _quitButton;
}
- (UILabel *)joinCourseLabel {
    if (!_joinCourseLabel) {
        _joinCourseLabel = [[UILabel alloc] init];
        _joinCourseLabel.text = @"参与的课程:";
        _joinCourseLabel.textColor = RGB_COLOR(57, 182, 222, 1);
        _joinCourseLabel.font = [UIFont systemFontOfSize:25];
    }
    return _joinCourseLabel;
}
- (UICollectionViewFlowLayout *)layout {
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        
        _layout.itemSize = CGSizeMake(SCREEN_WIDTH * 0.23, SCREEN_WIDTH * 0.23 * 0.55);
        _layout.minimumInteritemSpacing = 10;
        _layout.minimumLineSpacing = 10;
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    return _layout;
}
- (UICollectionView *)joinCourseCollectionView {
    if (!_joinCourseCollectionView) {
        _joinCourseCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.layout];
        
        _joinCourseCollectionView.backgroundColor = [UIColor whiteColor];
        
        // 设置数据源
        _joinCourseCollectionView.dataSource = self;
        
        // 注册单元格
        [_joinCourseCollectionView registerClass:[JoinCourseCollectionViewCell class] forCellWithReuseIdentifier:kJoinCourseCollectionViewCellIdentifier];
    }
    return _joinCourseCollectionView;
}

@end
